package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.common.Result;
import com.example.common.ResultStatus;
import com.example.pojo.NewPhoto;
import com.example.pojo.Share;
import com.example.service.NewPhotoService;
import com.example.service.OldPhotoService;
import com.example.service.ShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Map;



@RestController
@CrossOrigin
@RequestMapping("/share")
@Slf4j
public class ShareController {
    @Autowired
    ShareService shareService;
    @Autowired
    NewPhotoService photoService;
    @Autowired
    OldPhotoService oldPhotoService;

    /**
     * 发布新朋友圈
     * @param params
     * @return
     */
    @RequestMapping("/upcircle")
    @ResponseBody
    public String publish(@RequestParam Map<NewPhoto, Object> params, Share share, String content, String shareType, MultipartFile file,JSONObject json) throws IOException {
        Date time = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String current = sdf.format(time);
        share.setShareTime(Date.valueOf(current));//设置生成朋友圈时间
        share.setContent(content);//朋友圈文案
        share.setShareType(shareType);//设置朋友圈类型
        //获取添加的朋友圈图片url
        String photoPath= null;//将上传的图片上传到存储空间
        photoPath = (String) oldPhotoService.uploadPath(file);
        oldPhotoService.addPhoto(json,photoPath);//将上传的图片写入数据库
        //photoService.getPhotoUrlByUserId();
        if(photoPath!=null){
            return ResultStatus.SUCCESS.toString();
        }else {
            return ResultStatus.ERROR.toString();
        }
    }

    /**
     * 删除朋友圈
     * @param share
     * @return
     */
    @RequestMapping("/deletecircle")
    @ResponseBody
    public String delete(Share share) {
        shareService.deleteShare(share.getShareId());
        return ResultStatus.SUCCESS.toString();
    }
}
